from pydantic import BaseModel, model_validator
from typing import Union, List, Dict
from datetime import datetime
import json


class AOILotItem(BaseModel):
    page_index: int
    page_size: int
    lot_name: Union[str, None] = None


class AOILotUniqItem(BaseModel):
    lot_name: Union[str, None] = None
    is_complete: Union[int, None] = None


class FailureImageItem(BaseModel):
    page_index: Union[int, None] = None
    page_size: Union[int, None] = None
    lot_name: Union[str, None] = None
    server_hosts: Union[list, None] = None
    lot_code: Union[str, None] = None
    panel_ids: Union[list, None] = None
    panel_id: Union[str, None] = None
    panel_code: Union[str, None] = None
    failure_ids: Union[list, None] = None
    judgments: Union[list, None] = None
    start_time: Union[datetime, None] = None
    end_time: Union[datetime, None] = None
    ai_result: Union[int, None] = None
    aoi_result: Union[int, None] = None
    pjudgment: Union[str, None] = None

    aoi_start_time: Union[datetime, None] = None 
    aoi_end_time: Union[datetime, None] = None 


class PanelParetoItem(BaseModel):
    start_time: Union[datetime, None] = None
    end_time: Union[datetime, None] = None
    material_names: Union[list, None] = None
    server_hosts: Union[list, None] = None


class SelectPanelCodeItem(BaseModel):
    lot_name: Union[str, None] = None
    panel_id: Union[str, None] = None
    failure_ids: Union[list, None] = None
    judgments: Union[list, None] = None
    start_time: Union[datetime, None] = None
    end_time: Union[datetime, None] = None
    ai_result: Union[int, None] = None


class AOILotDetailItem(BaseModel):
    page_index: int
    page_size: int
    lot_name: Union[str, None] = None
    server_hosts: Union[list, None] = None
    material_names: Union[list, None] = None
    start_time: Union[datetime, None] = None
    end_time: Union[datetime, None] = None


class AOILotNGItem(BaseModel):
    lot_code: str
    page_index: int
    ng_page_index: int
    page_size: int
    failures: Union[list, None] = None


class AOIImageItem(BaseModel):
    image_code: str


class PanelItem(BaseModel):
    panel_id: str
    lot_name: Union[str, None] = None
    index: int = 0
    material_name: Union[str, None] = None


class FailureJudgeItem(BaseModel):
    failure_code: str
   

class PanelExportItem(BaseModel):
    panel_code: list
    export_type: int


class AOINextImageItem(BaseModel):
    image_code: str
    next_previous: str


class LotReportItem(BaseModel):
    lot_code: str

class LotDeleteItem(BaseModel):
    lot_code: list

class AOILotInferItem(BaseModel):
    material_code: str

class AOIPanelInferItem(BaseModel):
    material_code: Union[str, None] = None
    chip_info: Union[dict, None] = None


class AOIWaferInferItem(BaseModel):
    distance: float
    set_num_zhenhen: int
    set_diagonal_length: int
    set_area: int
    set_per_area: float
    area_scale: int


class UploadCsvItem(BaseModel):
    lots: List[dict]

    @model_validator(mode='before')
    @classmethod
    def validate_to_json(cls, value):
        if isinstance(value, str):
            return cls(**json.loads(value))
        return value
    

class ClearItem(BaseModel):
    ctype: int


class PanelImageItem(BaseModel):
    material_name: Union[str, None] = None
    material_names: Union[list, None] = None
    failure_ids: Union[list, None] = None
    start_time: Union[datetime, None] = None
    end_time: Union[datetime, None] = None
    page_index: Union[int, None] = None
    page_size: Union[int, None] = None
    server_hosts: Union[list, None] = None
    # 0: 按小时(一天的四个6小时) 1: 按天(7天) 2: 按周(8周)
    period_type: Union[int, None] = None
    top_3: Union[int, None] = None
    static: Union[bool, None] = None
    chip_type: Union[str, None] = None


class NewMaterialItem(BaseModel):
    material: Dict

    @model_validator(mode='before')
    @classmethod
    def validate_to_json(cls, value):
        if isinstance(value, str):
            return cls(**json.loads(value))
        return value
    

class DeleteMaterialItem(BaseModel):
    material_codes: List


class AllMaterialItem(BaseModel):
    material_name: Union[str, None] = None
    start_time: Union[datetime, None] = None
    end_time: Union[datetime, None] = None
    page_index: int
    page_size: int
    server_hosts: Union[list, None] = None


class ConfItem(BaseModel):
    ctype: int
    scp_type: int
    infer_type: int


class RedoScpJobItem(BaseModel):
    job_code: str


class AddAOIItem(BaseModel):
    hostname: str
    port: str = '22'
    username: str
    password: str
    lotdir: str
    aoi_name: Union[str, None] = None
    logdir: str


class DeleteAOIItem(BaseModel):
    hostname: str


class LoginUserItem(BaseModel):
    username: str
    password: str


class AddUserItem(BaseModel):
    username: str
    password: str
    role_code: str


class ModifyUserItem(BaseModel):
    user_code: str
    username: str
    password: str
    role_code: str


class DeleteUserItem(BaseModel):
    user_code: str


class UserAllItem(BaseModel):
    page_index: int
    page_size: int


class LotRenameItem(BaseModel):
    lot_code: str
    lot_name: str
    comment: Union[str, None] = None


class PanelHistoryItem(BaseModel):
    material_name: Union[str, None] = None
    lot_name: Union[str, None] = None
    panel_id: Union[str, None] = None


class UpdateMESItem(BaseModel):
    lot_codes: Union[list, None] = None


class JudementItem(BaseModel):
    query_str: Union[str, None] = None
